<template>
  <el-dialog title="设置角色" :visible.sync="dialogVisible">
    <el-form>
      <role-select v-model="formData.roleIds" :selected="selected" />
    </el-form>
    <div slot="footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import RoleSelect from '@/pages/system/role/roleSelect'
  import request from '@/plugin/axios'
  import postRequest from '@/libs/axiosSupport'
  export default {
    data() {
      return {
        dialogVisible: false,
        selected: [],
        formData: {}
      }
    },
    methods: {
      save() {
        this.loading = true;
        postRequest(
          '/users/setRoles',
          this.formData
        ).then(res => {
          this.$message({
            type: 'success',
            message: '设置成功'
          });
          this.loading = false;
          this.dialogVisible = false;
        })
      },
      open(row) {
        this.dialogVisible = true;
        this.formData={};
        this.selected=[];
        this.queryRolesByUserId(row);
        Object.assign(this.formData, {
          userId: row.id
        })
      },
      queryRolesByUserId(row) {
        request({
          url: '/roles/queryRolesByUserId',
          method: 'get',
          params: {
            'userId': row.id
          }
        }).then(res => {
          if (res.length > 0) {
            this.selected = res.map(s => s.id);
          }
        })
      }
    },
    components: {
      RoleSelect
    },
    mounted() {
    }
  }
</script>
